package com.caiyi.financial.nirvana.sms.gateway.sgip.message.body;

import com.caiyi.financial.nirvana.sms.gateway.sgip.message.SGIPBody;
import io.netty.buffer.ByteBuf;

import java.util.HashMap;
import java.util.Map;

/**
 * 连接回复消息
 */
public class SGIPBindResp extends SGIPBody {

    public final static Map<Integer, String> STATUS = new HashMap<>();

    //登录状态
    private byte status;

    static {
        STATUS.put(0, "正确");
        STATUS.put(1, "登录失败");
        STATUS.put(2, "重复登录");
        STATUS.put(3, "连接过多");
        STATUS.put(4, "登录类型错");
        STATUS.put(5, "其他错误");
    }

    @Override
    public int encode(ByteBuf buffer) {

        int length = 0;

        buffer.writeByte(this.status);
        length += 1;
        //8位保留位
        buffer.writeZero(8);
        length += 8;

        return length;
    }

    /**
     * 消息注册解码
     */
    @Override
    public SGIPBody decode(ByteBuf buffer) {
        if (buffer.readableBytes() < 9)
            throw new IllegalAccessError("可读字节过小,不足以支持解码");

        status = buffer.readByte();
        buffer.readBytes(8);
        return this;
    }

    public void setStatus(byte status) {
        this.status = status;
    }

    /**
     * @return 登录状态
     */
    public int getStatus() {
        return status;
    }

}
